import { enumStrokeColor } from '../theme'

const getEndModel = CircleNodeModel => {
  class EndModel extends CircleNodeModel {
    static extendKey = 'EndModel'
    constructor(data, graphModel) {
      if (data.text && typeof data.text === 'string') {
        data.text = {
          value: data.text,
          x: data.x,
          y: data.y + 35,
        }
      }
      super(data, graphModel)
    }
    getConnectedSourceRules() {
      const rules = super.getConnectedSourceRules()
      const notAsSource = {
        message: '结束节点不能作为连线的起点',
        validate: () => false,
      }
      rules.push(notAsSource)
      return rules
    }
  }
  return EndModel
}

const getEndNode = (CircleNode, h) => {
  class EndNode extends CircleNode {
    static extendKey = 'EndNode'
    getShape() {
      const { x, y, fill, stroke, strokeWidth, r, properties = {} } = this.getAttributes()
      const pStroke = enumStrokeColor[properties.type] || stroke
      return h(
        'g',
        {},
        h('circle', {
          cx: x,
          cy: y,
          fill,
          stroke: pStroke,
          strokeWidth,
          r,
        }),
        h('circle', {
          cx: x,
          cy: y,
          fill,
          stroke: pStroke,
          strokeWidth,
          r: r - 5,
        })
      )
    }
  }
  return EndNode
}

export const registerEndNode = ({ CircleNode, CircleNodeModel, h }) => ({
  model: getEndModel(CircleNodeModel),
  view: getEndNode(CircleNode, h),
})
